package com.google.code.irouter.portal.client.dashboard.main.center.managers.log;

import com.google.code.irouter.portal.client.AppConstants;
import com.google.code.irouter.portal.client.dashboard.main.north.menu.switcher.Solutions;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.gwtext.client.core.SortDir;
import com.gwtext.client.core.UrlParam;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.HttpProxy;
import com.gwtext.client.data.IntegerFieldDef;
import com.gwtext.client.data.JsonReader;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.util.Format;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.PagingToolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.GridView;
import com.gwtext.client.widgets.grid.RowParams;
import com.gwtext.client.widgets.grid.RowSelectionModel;

public final class Browser extends GridPanel {

	private static final AppConstants CONSTANTS = GWT.create(AppConstants.class);
	private boolean showPreview = false;

	public Browser() {
		super();
		this.setId(Browser.class.getName());

		HttpProxy dataProxy = new HttpProxy("/log/show");

		final RecordDef recordDef = new RecordDef(new FieldDef[] {
				new StringFieldDef("category"), 
				new StringFieldDef("message"), 
				new StringFieldDef("level"), 
				new StringFieldDef("sender"), 
				new StringFieldDef("solution"),
				new StringFieldDef("stack"),
				new StringFieldDef("createDate"),
				new IntegerFieldDef("key"), 
				}
		);
		JsonReader reader = new JsonReader(recordDef);
		reader.setRoot("topics");
		reader.setTotalProperty("totalCount");
		reader.setId("key");

		final Store store = new Store(dataProxy, reader, true);
		store.setDefaultSort("createDate", SortDir.DESC);
		
		String solutionId = "";
		String cookieId = Cookies.getCookie(Solutions.COOKIE_SOLUTION);
		if(cookieId != null)
			if(cookieId.trim().length() > 0)
				solutionId = cookieId;
		
		UrlParam[] baseParams = store.getBaseParams();
		baseParams[baseParams.length] = new UrlParam("solution", solutionId);
		store.setBaseParams(baseParams);

		ColumnConfig titleColumn = new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_log_browser_column_title(), "message", 420, true);
		titleColumn.setCss("white-space:normal;");
		ColumnConfig categoryColumn = new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_log_browser_column_category(), "category", 60, true);
		ColumnConfig levelColumn = new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_log_browser_column_level(), "level", 60, true);
		ColumnConfig senderColumn = new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_log_browser_column_sender(), "sender", 240, true);
		ColumnConfig solutionColumn = new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_log_browser_column_solution(), "solution", 70, true);
		ColumnConfig createColumn = new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_log_browser_column_create(), "createDate", 170, true);

		ColumnModel columnModel = new ColumnModel(
				new ColumnConfig[] { 
						titleColumn,
						categoryColumn,
						levelColumn,
						senderColumn,
						solutionColumn,
						createColumn
				});
		columnModel.setDefaultSortable(true);

		this.setStore(store);
		this.setColumnModel(columnModel);
		this.setLoadMask(true);
		this.setSelectionModel(new RowSelectionModel());
		this.setStripeRows(true);
		this.setBorder(false);

		GridView view = new GridView() {
			public String getRowClass(Record record, int index,
					RowParams rowParams, Store store) {
				if (showPreview) {
					rowParams.setBody(Format.format("<p>{0}</p>",
							record.getAsString("stack")));
					return "x-grid3-row-expanded";
				} else {
					return "x-grid3-row-collapsed";
				}
			}
		};
		view.setForceFit(true);
		view.setEnableRowBody(true);
		this.setView(view);

		PagingToolbar pagingToolbar = new PagingToolbar(store);
		pagingToolbar.setPageSize(35);
		pagingToolbar.setDisplayInfo(true);
		pagingToolbar.setDisplayMsg("Displaying topics {0} - {1} of {2}");
		pagingToolbar.setEmptyMsg("No topics to display");

		pagingToolbar.addSeparator();
		ToolbarButton toolbarButton = new ToolbarButton("Show Preview");
		toolbarButton.setPressed(showPreview);
		toolbarButton.setEnableToggle(true);
		toolbarButton.setCls("x-btn-text-icon details");
		toolbarButton.addListener(new ButtonListenerAdapter() {
			public void onToggle(Button button, boolean pressed) {
				toggleDetails(pressed);
			}
		});

		pagingToolbar.addButton(toolbarButton);
		this.setBottomToolbar(pagingToolbar);	
		store.load(0, 35);
	}

	private void toggleDetails(boolean pressed) {
		showPreview = pressed;
		this.getView().refresh();
	}

}
